package com.maven.zh.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.maven.zh.bean.Manager;
import com.maven.zh.bean.Menu;
import com.maven.zh.bean.Permission;
import com.maven.zh.service.ManagerService;
import com.maven.zh.service.MenuService;
import com.maven.zh.service.PermissionService;
import com.maven.zh.util.Const;


/**
 * 页面跳转控制器
 * @author 18801
 *
 */
@Controller
@RequestMapping("/")
public class ForwardController {

	@Autowired
	private MenuService menuService;
	
	@Autowired
	private ManagerService managerService;
	
	@Autowired
	private PermissionService permissionService;
	
	@RequestMapping("/gotofile")
	public String fileTest(){
		return "testjsp";
	}
	/**
	 * 退出系统
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session,HttpServletRequest req,HttpServletResponse res) {
		// 会话失效
		session.invalidate();
		//删除cookie中存储的数据
		Cookie[] cookies = req.getCookies();
		for(int i = 0,len = cookies.length; i < len; i++) {
			Cookie cookie = new Cookie(cookies[i].getName(),null);
			cookie.setMaxAge(0);
			cookie.setPath("/");
			res.addCookie(cookie);
		}
		
		// 跳转页面，回到登陆页面
		return "redirect:/";
	}
	
	/**
	 * 跳转到登录页面
	 * @param req
	 * @param session
	 * @return
	 */
	@RequestMapping("/login")
	public String login( HttpServletRequest req, HttpSession session ) {
		
		//是否需要登录的标识
		boolean needlogin = true;
		
		String logintype = "";
		Cookie[] cs = req.getCookies();
		if ( cs != null ) {
			for ( Cookie c : cs ) {
				if ( "logincode".equals(c.getName()) ) {
					// 自动登陆的场合
					String code = c.getValue();
					// acct=admin&userpswd=admin
					String[] ss = code.split("&");
					String loginacct = ss[0].split("=")[1];
					String userpswd = ss[1].split("=")[1];
				
					Manager manager = new Manager();
					manager.setLoginacct(loginacct);
					manager.setUserpswd(userpswd);
					Manager dbManager = managerService.queryManager4Login(manager);
					if ( dbManager == null ) {
						needlogin = true;
					} else {
						needlogin = false;
						session.setAttribute(Const.SESSION_MANAGER, dbManager);
					}
				}
			}
		}

		if ( needlogin ) {
			return "login";
		} else {
			
			return "redirect:main.htm";
			
		}
	}
	
	
	/**
	 * 跳转到主页面
	 * @return
	 */
	@RequestMapping("/main")
	public String main(HttpSession session) {
		
		/*Menu root = new Menu();
        root.setId(0);
        queryMenus(root);
        model.addAttribute(root);*/
		
		//查询登录的用户信息
		Manager loginManager = (Manager)session.getAttribute(Const.SESSION_MANAGER);
		if(loginManager ==null){
			return "login";
		}
		//根据登录的用户查询该用户的权限信息
		List<Permission> permissions = managerService.queryPermissionsByManager(loginManager);
		//先读取父权限
		List<Permission> parent = new ArrayList<Permission>();
		for(Permission permission:permissions){
			if(permission.getPid()==0){
				parent.add(permission);
			}
		}
		
		//*************************************
		//然后根据每一个父权限读取对应的子权限数据
		
		for(Permission parentPermission:parent){
			
			
			List<Permission> childrenPermissions = new ArrayList<Permission>();
			
			//List<Permission> children=parentPermission.getChildren();
			
			for (Permission permission : permissions) {
				if(parentPermission.getId() == permission.getPid()){
					
					childrenPermissions.add(permission);
				}
			
			
		    }
			
			parentPermission.setChildren(childrenPermissions);
			
            //将查询的子权限作为父权限
			
			List<Permission> parentPermissions= childrenPermissions;
			
			
			for(Permission parentPermission2:parentPermissions){
				
				List<Permission> childrenPermissions2 = new ArrayList<Permission>();
				
				for (Permission permission : permissions) {
					
					if(parentPermission2.getId() == permission.getPid()){
						
						childrenPermissions2.add(permission);
					}
				}
				//将父权限与子权限建立关系
				parentPermission2.setChildren(childrenPermissions2);
			}
			
			
			
			
			
			
			//List<Permission> childrenPermissions =permissionService.queryChildPersisson(parentPermission);
			
			//List<Permission> children=parentPermission.getChildren();
			//for (Permission permission : children) {
				//System.out.println(permission);
			//}
			//将父权限与子权限建立关系
			//parentPermission.setChildren(childrenPermissions);
			
			//*************************************
			//将查询的子权限作为父权限
			
			//List<Permission> parentPermissions= childrenPermissions;
			
			//for(Permission parentPermission2:parentPermissions){
				
				//List<Permission> childrenPermissions2 = permissionService.queryChildPersisson(parentPermission2);
				//for (Permission permission : childrenPermissions2) {
					
					
				//}
				//将父权限与子权限建立关系
				//parentPermission2.setChildren(childrenPermissions2);
			//}
		}
		
		session.setAttribute("smenu", parent);
		return "main2";
		
		/*// 读取菜单数据
		// 先读取父菜单数据
		List<Menu> parentMenus = menuService.queryParentMenu();
		// **********************************************************
		// 然后再根据每一个父菜单读取相应的子菜单数据
		for ( Menu pmenu : parentMenus ) {
			List<Menu> childrenMenus = menuService.queryChildMenu(pmenu);
			
			// 将父菜单和子菜单建立关系
			pmenu.setChildrenMenus(childrenMenus);
			
			// ****************************************************-******
			// 将查询的子菜单当成父菜单
			List<Menu> parentMenus1 = childrenMenus;
			
			for ( Menu pmenu1 : parentMenus1 ) {
				List<Menu> childrenMenus1 = menuService.queryChildMenu(pmenu1);
				// 将父菜单和子菜单建立关系
				pmenu1.setChildrenMenus(childrenMenus1);
			}
		}
		
		session.setAttribute("smenu",parentMenus);
		return "main2";*/
	}
	
	//递归
		public void queryMenus( Menu pmenu ) {
			
			// 子菜单
			List<Menu> childrenMenus = menuService.queryChildMenu(pmenu);
			
			// 继续查询子菜单
			for ( Menu childMenu : childrenMenus ) {
				queryMenus(childMenu);
			}
			
			// 整合父子菜单的关系
			pmenu.setChildrenMenus(childrenMenus);
		}
	

}
